﻿using Convertor.Interface;
using System;
using System.IO;
using System.Text;
using System.Windows;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.SaveToDisk
{
    public class Plugin : PluginBase
    {

        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.SaveToDisk;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.SaveToDisk;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_File") + " - " + UILanguage.FindResource("Plugin_SaveToDisk_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_SaveToDisk_Desc"); }
        }       

        #endregion

        #region IConvertParamters 成员

        private int errorCount = 0;
        public override IConvertParamters Output(IConvertParamters content)
        {
            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            if (content.LastInputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            if (Log != null) { Log(this, config.ToolTip, LogTypes.Normal); }

            string savePath = config.SaveFilePath;
            if (string.IsNullOrEmpty(config.SaveFilePath))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_OpenConfigForm"), LogTypes.Normal | LogTypes.HidePluginName); }
                if (Loader.ShowDialog() == true)
                {
                    config = ((IFormGetConfigEvent)Loader.Loader).GetConfig() as ConfigContent;
                }
                else
                {
                    if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_CloseConfigForm"), LogTypes.Error | LogTypes.HidePluginName); }
                    content.StopConvert = true;
                    return content;
                }
            }

            if (string.IsNullOrEmpty(config.SaveFilePath))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_SaveToDisk_SavePathEmpty"), LogTypes.Error | LogTypes.HidePluginName); }
                content.StopConvert = true;
                return content;
            }

            try
            {
                string inputPath = content.LastInputContent.ToString().Trim();
                savePath = Path.Combine(config.SaveFilePath, inputPath);
                if (savePath.Length > 248) //超出长度
                {
                    savePath = Path.Combine(config.SaveFilePath, Guid.NewGuid().ToString("N"));
                }
                else
                {
                    FileInfo f = new FileInfo(savePath);
                    string ext = f.Extension;
                    if (string.IsNullOrEmpty(ext)) //没有扩展名
                    {
                        savePath = Path.Combine(config.SaveFilePath, Guid.NewGuid().ToString("N"));
                    }
                }
                if (content.OutputContent is string || content.OutputContent is StringBuilder)
                {
                    File.WriteAllText(savePath, content.OutputContent.ToString(), config.FileEncoding);
                }
                else if ((content.OutputContent as MemoryStream) != null)
                {
                    MemoryStream ms = content.OutputContent as MemoryStream;
                    FileStream file = new FileStream(savePath, FileMode.Create, System.IO.FileAccess.Write);
                    byte[] bytes = ms.ToArray();
                    file.Write(bytes, 0, bytes.Length);
                    file.Close();
                }
                if (Log != null) { Log(this, string.Format(UILanguage.FindResource("Plugin_SaveToDisk_ConvertSuccess"), savePath, content.LastInputContent.ToString()), LogTypes.Normal | LogTypes.HidePluginName); }
            }
            catch (Exception ex)
            {
                errorCount++;
                if (errorCount == 1)
                {
                    if (Loader.ShowDialog() == true)
                    {
                        config = ((IFormGetConfigEvent)Loader.Loader).GetConfig() as ConfigContent;
                        ((IConvert)this).Output(content);
                    }
                }
                if (Log != null) { Log(this, ex.ToString(), LogTypes.Alert | LogTypes.HidePluginName); }
            }

            return new ConvertParamters(content.LastInputContent, savePath);
        }

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

    }
}
